﻿
namespace ShpMerger
{
    internal class Record
    {
        private int infoCount;
        public Header InfoHeader { private set; get; }
        private int recordNumber;
        private int recordLength;
        private byte[] recordContents;

        public Record()
        {
            InfoHeader = new Header();
            infoCount = 1;
        }
        public int RecordNumber
        {
            get
            {
                return recordNumber;
            }
            set
            {
                recordNumber = value;
            }
        }
        public void Read(ExtendBinaryReader extendBinaryReader)
        {
            recordNumber = extendBinaryReader.ReadInt32UsingBigEndian();
            recordLength = extendBinaryReader.ReadInt32UsingBigEndian();
            recordContents = extendBinaryReader.ReadBytes(recordLength * 2);
        }

        public void Write(ExtendBinaryWriter extendBinaryWriter)
        {
            extendBinaryWriter.WriteUsingBigEndian(recordNumber);
            extendBinaryWriter.WriteUsingBigEndian(recordLength);
            extendBinaryWriter.Write(recordContents);
        }
        public void AddInfo(ExtendBinaryReader binaryReader, 
                ExtendBinaryWriter binaryWriter)
        {
            InfoHeader.ReadInfoHeader(binaryReader);
            InfoHeader.InfoNumber = infoCount;
            InfoHeader.WriteInfoHeader(binaryWriter);
            ++ infoCount;
            for (int i = 1; i <= InfoHeader.InfoLength / 2; i++)
            {
                binaryWriter.Write(binaryReader.ReadInt32());
            }
        }
    }
}
